package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.model.TimeModel;

/**
 * <p>
 * Title:
 * </p>
 * 时间标签适配器
 * <p>
 * Description:
 * </p>
 * 
 * @author zhaofei
 * @date 2017年1月6日 下午2:37:49
 */
public class TimeLabelAdapter extends BaseAdapter {

	private ArrayList<TimeModel> list;

	public TimeLabelAdapter(ArrayList<TimeModel> list) {
		this.list = list;
	}

	public void SelectPosition(int position) {
		if (null != list) {
			for (int i = 0; i < list.size(); i++) {
				list.get(i).setIsSelect(false);
			}
			if (position >= 0) {
				list.get(position).setIsSelect(true);
			}
			notifyDataSetChanged();
		}
	}

	public void SetData(ArrayList<TimeModel> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_time_label, null);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView.findViewById(R.id.textview);
			holder.bg_color = (RelativeLayout) convertView.findViewById(R.id.bg_color);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		TimeModel model = list.get(position);
		holder.textView.setText(model.getName());
		if (model.getIsSelect()) {
			holder.bg_color.setBackgroundResource(R.drawable.shape_analysis_time2);
			holder.textView.setTextColor(Color.parseColor("#f87242"));
		} else {
			holder.bg_color.setBackgroundResource(R.drawable.shape_analysis_time);
			holder.textView.setTextColor(Color.parseColor("#5A5A5A"));
		}

		return convertView;
	}

	static class ViewHolder {
		TextView textView;
		RelativeLayout bg_color;
	}

}
